<?php
//包含数据库配置文件
include '../../public/dbconfig.php';
//包含函数库文件
include '../../public/functions.php';
//设置字符集
header('Content-type:text/html;charset=utf-8');
//接收用户传过来的值
//var_dump($_POST);

//接收用户名
$username = $_POST['username'];
//密码
$pass = $_POST['pass'];
//确认密码
$repass = $_POST['repass'];
//性别
$sex = $_POST['sex'];
//学历
$xueli = $_POST['xueli'];
//权限
$qx = $_POST['qx'];
//获取时间
$rtime = time();
//获取ip
$ip = $_SERVER['REMOTE_ADDR'];

//判断用户名长度
$userlen = strlen($username);
if($userlen < 6 || $userlen > 12){
	echo '<script>alert("用户名长度应该在6-12个字符之间");window.location.href="add.php";</script>';
	exit;
}

//判断密码长度
$passlen = strlen($pass);
if($passlen < 6 || $passlen > 18){
	echo '<script>alert("密码长度应该在6-18个字符之间");window.location.href="add.php";</script>';
	exit;
}

//判断密码与确认密码是否一致
if($pass == $repass){
	$password = md5($pass);
}else{
	echo '<script>alert("密码和确认密码不一致");window.location.href="add.php";</script>';
	exit;
}

//把获取到的ip转换为整型
if($ip == '::1'){
	$rip = ip2long('127.0.0.1');
}else{
	$rip = ip2long($ip);
}

//链接数据库
$link = con();
//准备SQL
$sql = "insert into user(username,password,sex,xueli,rtime,rip,qx)values('{$username}','{$password}','{$sex}','{$xueli}','{$rtime}','{$rip}','{$qx}')";
//echo $sql;
//exit;

//执行SQL
$res = mysqli_query($link, $sql);

if(mysqli_affected_rows($link) > 0){
	//判断执行结果
	if($res){
		echo '<script>alert("添加成功");window.location.href="list.php";</script>';
	}else{
		echo '<script>alert("添加失败");window.location.href="add.php";</script>';
	}
}else{
	echo '<script>alert("用户名已存在");window.location.href="add.php";</script>';
}



//关闭数据库链接
mysqli_close($link);

